#include <algorithm>
#include <cassert>
#include <vector>

using namespace std;

class Solution {
public:
    int minEatingSpeed(vector<int>& piles, int h) {
        int left = 1, right = *max_element(piles.begin(), piles.end());
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (calculateSum(piles, mid) > h) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }

        return left;
    }

private:
    int calculateSum(vector<int>& piles, int speed) {
        int sum = 0;
        for (int pile : piles) {
            sum += (pile + speed - 1) / speed;
        }

        return sum;
    }
};

int main() {
    Solution s;

    vector<int> piles1{3, 6, 7, 11}, piles2{30, 11, 23, 4, 20},
        piles3{30, 11, 23, 4, 20};

    assert(s.minEatingSpeed(piles1, 8) == 4);
    assert(s.minEatingSpeed(piles2, 5) == 30);
    assert(s.minEatingSpeed(piles3, 6) == 23);

    return 0;
}